/*
 * BeaconSensor.java
 *
 * Created on September 8, 2008, 12:27 AM
 * 
 * index:
 * 0 -> REDBUOY
 * 1 -> GREENBUOY
 * 2 -> FORCEFIELD
 *
 * This is the IRobot Create's Infrared Beacon Sensor
 * For more read the Create's Documentation
 */
package org.gunncs.actoriface;

import com.iaroc.irobot.IRobotCreate;
import com.iaroc.irobot.IRobotCreateConstants;
import java.io.IOException;

/**
 *
 * @author Anandg
 */
public class BeaconSensor extends Sensor {

    public static final int REDBUOY = 0;
    public static final int GREENBUOY = 1;
    public static final int FORCEFIELD = 2;
    public static final double ABSENT = 0.0;
    public static final double PRESENT = 1.0;
    IRobotCreate create;

    /** Creates a new instance of BeaconSensor */
    public BeaconSensor(IRobotCreate c) {
        create = c;
        double[] d = new double[4];
        d[0] = 0.0;
        d[1] = 0.0;
        d[2] = 0.0;
        d[3] = Simulator.time();
        setState(d);
    }

    public String getName() {
        return "BeaconSensor";
    }

    public int getID() {
        return 3;
    }

    public synchronized void update(Thread daemon) throws InterruptedException, IOException {
        if (create == null) {
            return;
        }
        double[] d = new double[4];
        d[REDBUOY] = ABSENT;
        d[GREENBUOY] = ABSENT;
        d[FORCEFIELD] = ABSENT;
        int rawByte = create.getInfraredByte(); //Refer to IRobotCreateConstants.java
        switch (rawByte) {
            case IRobotCreateConstants.INFRARED_BYTE_NONE:
                break;
            case IRobotCreateConstants.INFRARED_BYTE_HOME_BASE_FORCE_FIELD:
                d[FORCEFIELD] = PRESENT;
                break;
            case IRobotCreateConstants.INFRARED_BYTE_HOME_BASE_RED_BUOY:
                d[REDBUOY] = PRESENT;
                break;
            case IRobotCreateConstants.INFRARED_BYTE_HOME_BASE_GREEN_BUOY:
                d[GREENBUOY] = PRESENT;
                break;
            case IRobotCreateConstants.INFRARED_BYTE_HOME_BASE_RED_GREEN_BUOY_FORCE_FIELD:
                d[FORCEFIELD] = PRESENT;
                d[REDBUOY] = PRESENT;
                d[GREENBUOY] = PRESENT;
                break;
            case IRobotCreateConstants.INFRARED_BYTE_HOME_BASE_GREEN_BUOY_FORCE_FIELD:
                d[GREENBUOY] = PRESENT;
                d[FORCEFIELD] = PRESENT;
                break;
            case IRobotCreateConstants.INFRARED_BYTE_HOME_BASE_RED_BUOY_FORCE_FIELD:
                d[FORCEFIELD] = PRESENT;
                d[REDBUOY] = PRESENT;
                break;
            case IRobotCreateConstants.INFRARED_BYTE_HOME_BASE_RED_GREEN_BUOY:
                d[REDBUOY] = PRESENT;
                d[GREENBUOY] = PRESENT;
                break;
        }
        d[3] = (double) Simulator.time();
        //Simulator.out.println("                   " +d[0]);
        setState(d);
    }
}
